package com.yiqi.liebang.feature.wallet.view;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.BanlanceBo;
import com.yiqi.liebang.entity.bo.PayBo;
import com.yiqi.liebang.entity.bo.PublicBo;
import com.yiqi.liebang.feature.setting.view.WithdrawBidingPhone;
import com.yiqi.liebang.feature.wallet.contract.WalletContract;
import com.yiqi.liebang.feature.wallet.dependencies.wallet.DaggerWalletComponent;
import com.yiqi.liebang.feature.wallet.dependencies.wallet.WalletPresenterModule;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class WithdrawActivity extends BaseActivity implements WalletContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    WithdrawDialog withdrawDialog;
    @BindView(R.id.iv_withdraw_logo)
    ImageView mIvWithdrawLogo;
    @BindView(R.id.tv_withdraw_name)
    TextView mTvWithdrawName;
    @BindView(R.id.withdraw_total)
    TextView withdrawTotal;
    @BindView(R.id.btn_recharge_next)
    TextView btnRechargeNext;
    @BindView(R.id.wall_money)
    PowerfulEditText wallMoney;
    @BindView(R.id.btn_all)
    TextView btnAll;
    @BindView(R.id.iv_pay_alipay)
    ImageView ivPayAlipay;
    @BindView(R.id.btn_pay_alipay)
    RelativeLayout btnPayAlipay;
    private double mMoney;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_withdraw;
    }

    @Inject

    WalletContract.Presenter mPresenter;

    @Override
    protected void initInjector() {
        DaggerWalletComponent.builder().walletPresenterModule(new WalletPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "提现", true, true);
        withdrawDialog = new WithdrawDialog(this);
    }

    @Override
    protected void initData() {
        mPresenter.getUserAssetsDetail(1, 10);
    }

    int payStatus;

    @OnClick({R.id.btn_pay_alipay, R.id.btn_recharge_next, R.id.withdraw_total, R.id.btn_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_pay_alipay:
                withdrawDialog.show();
                withdrawDialog.setListener(new WithdrawDialog.MyClickListenerInterface() {
                    @Override
                    public void doConfirm(int type) {
                        payStatus = type;
                        mIvWithdrawLogo.setImageResource(type == 0 ? R.drawable.list_icon_zhifubao : R.drawable.list_icon_weixinzhifu);
                        mTvWithdrawName.setText(type == 0 ? "支付宝" : "微信");
                    }
                });
                break;
            case R.id.btn_recharge_next:
                if (TextUtils.isEmpty(wallMoney.getText().toString())) {
                    T.showShort("请输入金额！");
                    return;
                }

                if (Double.valueOf(wallMoney.getText().toString())>mMoney) {
                    T.showShort("输入金额超出可提现范围！");
                    return;
                }
                if(payStatus==1){
                    mPresenter.userWithdraw(payStatus, Double.valueOf(wallMoney.getText().toString()), "","");
                }else{
                    Intent intent=new Intent(getApplicationContext(),WithdrawBidingPhone.class);
                    intent.putExtra("payType",payStatus);//支付方式
                    intent.putExtra("money",Double.valueOf(wallMoney.getText().toString()));
                    startActivityForResult(intent,60001);
                }

                break;
            case R.id.withdraw_total:
                break;
            case R.id.btn_all:
                wallMoney.setText(totalMoney);
                break;
        }
    }

    String totalMoney = "";

    @Override
    public void showSuccess(String error) {
        mMoney=Double.valueOf(error);
        withdrawTotal.setText("可用余额"+error);
        totalMoney = error;
    }

    @Override
    public void showPaySuccess(PayBo classifyBos) {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==30001){
            if(data!=null&& !TextUtils.isEmpty(data.getStringExtra("openId"))){
               //微信
                mPresenter.userWithdraw(payStatus, Double.valueOf(wallMoney.getText().toString()), "",data.getStringExtra("openId"));
            }
        }else if(requestCode==60001){
            if(data!=null&& !TextUtils.isEmpty(data.getStringExtra("alibb"))){
                finish();
            }
        }
    }

    @Override
    public void userWithdrawSucc(PublicBo entity) {
        if (entity.getStatus() == 1) {//1没有绑定 2绑定了
            Intent intent=new Intent(getApplicationContext(),WithdrawBidingPhone.class);
            intent.putExtra("payType",payStatus);//支付方式
            intent.putExtra("money",Double.valueOf(wallMoney.getText().toString()));
            startActivityForResult(intent,30001);
        } else if(entity.getStatus()==0){
            T.showShort("提现成功,请等待审核！");
            finish();
        }
    }

    @Override
    public void showTransactionListSuccess(List<BanlanceBo.ListData> workBoPageData) {

    }


    @Override
    public void showError(String error) {
        T.showShort(error);
    }

    @Override
    public void showTransactionListMoreSuccess(List<BanlanceBo.ListData> msg) {

    }


    @Override
    public void showTransactionListMoreError(String msg) {

    }

    @Override
    public void showTransactionListEmpty() {

    }
}
